python readline函数 阻塞 |
您所在的位置:网站首页 › subprocess readline › python readline函数 阻塞 |
我试图通过python2.7运行minecraft服务器,它运行得很好。 但是当我试图向它传递stop命令时,它不会做任何事情,直到服务器输出一些东西。在 这是我的代码:import os, sys, subprocess, threading, time class Server: def start(self): t = threading.Thread(target=self.run) t.daemon = True t.start() def run(self): self.p = subprocess.Popen('java -Xmx512M -Xms512M -jar minecraft_server.1.8.1.jar nogui', cwd=os.path.join(os.getcwd(), 'server'), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) while True: nextline = self.p.stdout.readline() print self.p.poll() if nextline == '' and self.p.poll() != None: break if not nextline == '': sys.stdout.write(nextline) sys.stdout.flush() def stop(self): self.p.communicate(input='stop')[0] #endclass s = Server() s.start() count = 0 # keep running while True: count += 1 if count == 15: s.stop() print "STOPPING SERVER" time.sleep(1) 输出图像: 我不想让它停在那里。在 15秒后停止它是为了测试我是否能让它正常工作,但我不知道如何修复这个问题。我看到了一些使用fcntl的解决方案,但我希望它能在所有平台上运行,所以这不是我的选择。在 我怎样才能使它在任何时候都能运行任何命令?在 更新: ^{pr2}$ 输出图像: |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |